import Vue from 'vue';
import Vuex from 'vuex';
import { getLayoutTabsStatus, setLayoutTabsStatus } from '@/utils';

import {
  loadAreaMapData,
  loadEnterpriseMapData,
  loadIndustryMapData,
  loadUnderEnterprise as getListEnterprise
} from '@/api/enterprose';

import user from '@/store/user';
import tagsView from '@/store/tags-view';
import { ENERGY_UNITS, ENERGY_VARIETY } from '@/config';
import { getTypeList } from '@/api/energy';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    showLayoutTabs: +getLayoutTabsStatus() === 1,
    logoTitle: process.env.VUE_APP_TITLE,
    isCollapsed: false,
    version: process.env.VUE_APP_VERSION,
    isNC: process.env.VUE_APP_IDENTIFY === 'nanchang',
    isYZ: process.env.VUE_APP_IDENTIFY === 'yangzhou',
    // --------------------------
    // 能源类型--新
    energyType: [],
    // --------------------------
    // 能源企业所属行业类别
    energyIndustry: [],
    // 能源种类（如：水、电、气、其他能源）
    energyVariety: ENERGY_VARIETY,
    energyUnits: ENERGY_UNITS,
    // 入驻系统的能耗企业列表（名称和ID）
    energyEnterpriseList: [],
    // 企业地图数据，各企业用水、电、气、综合能耗等数据
    enterpriseMapList: [],

    // 区域地图数据，各区域用水、电、气、综合能耗等数据
    districtMapList: [],

    // 行业地图数据，各行业用水、电、气、综合能耗等数据
    industryMapList: []
  },
  mutations: {
    updateEnergyType(state, list) {
      state.energyType = list;
    },
    SET_SHOW_LAYOUT_TABS(state, payload) {
      state.showLayoutTabs = payload;
      setLayoutTabsStatus(payload ? 1 : 0);
    },
    SET_LOGO_TITLE(state, payload) {
      state.logoTitle = payload;
    },
    SET_IS_COLLAPSED(state, payload) {
      state.isCollapsed = payload;
    },
    SET_ENERGY_INDUSTRY(state, payload) {
      state.energyIndustry = [...payload];
    },
    SET_ENERGY_ENTERPRISE_LIST(state, payload) {
      state.energyEnterpriseList = [...payload];
    },
    SET_ENTERPRISE_MAP_LIST(state, payload) {
      state.enterpriseMapList = [...payload];
    },
    SET_DISTRICT_MAP_LIST(state, payload) {
      state.districtMapList = [...payload];
    },
    SET_INDUSTRY_MAP_LIST(state, payload) {
      state.industryMapList = [...payload];
    }
  },
  actions: {
    async getEnergyType({ commit, state }) {
      // 使用缓存
      if (state.energyType.length > 0) return Promise.resolve();
      const { data: res, code } = await getTypeList();
      commit('updateEnergyType', code === 200 ? res : []);
    },
    GET_ENERGY_INDUSTRY({ commit, state }) {
      const energyIndustry = state.energyIndustry;
      if (!energyIndustry.length) {
        loadIndustryMapData().then(res => {
          if (res.code === 200) {
            res.data = res.data || [];
            commit(
              'SET_ENERGY_INDUSTRY',
              res.data.map(v => ({
                name: v.name,
                code: v.id,
                value: v.id,
                belongIndustry: v.id,
                belongIndustryName: v.name
              }))
            );
          }
        });
      }
    },
    async GET_ENTERPRISE_LIST({ commit, state }) {
      return new Promise(resolve => {
        const lists = state.energyEnterpriseList;
        if (!lists.length) {
          getListEnterprise().then(res => {
            if (res.code === 200) {
              res.data = res.data || [];
              const data = res.data.map(v => ({
                ...v,
                name: v.enterpriseName,
                value: v.id,
                id: v.id,
                enterpriseId: v.enterpriseId,
                longitude: v.longitude,
                latitude: v.latitude,
                districtBelong: v.districtBelong,
                districtBelongName: v.districtBelongName,
                belongIndustry: v.belongIndustry,
                belongIndustryName: v.belongIndustryName,
                position: [v.longitude, v.latitude]
              }));
              commit('SET_ENERGY_ENTERPRISE_LIST', data);
              resolve(data);
            }
          });
        } else {
          resolve(lists);
        }
      });
    },
    GET_ENTERPRISE_MAP_LIST({ commit, state }) {
      const lists = state.enterpriseMapList;
      if (!lists.length) {
        loadEnterpriseMapData().then(res => {
          if (res.code === 200) {
            res.data = res.data || [];
            commit(
              'SET_ENTERPRISE_MAP_LIST',
              res.data.map(v => ({
                _name: v.name,
                enterpriseId: v.id,
                enterpriseName: v.name,
                enterpriseNumber: v.enterpriseNumber,
                water: v.water,
                electric: v.electricity,
                coal: v.coal,
                gas: v.gas,
                consumption: v.comprehensiveEnergyConsumption
              }))
            );
          }
        });
      }
    },
    GET_DISTRICT_MAP_LIST({ commit, state }) {
      const lists = state.districtMapList;
      if (!lists.length) {
        loadAreaMapData().then(res => {
          if (res.code === 200) {
            res.data = res.data || [];
            commit(
              'SET_DISTRICT_MAP_LIST',
              res.data.map(v => ({
                _name: v.name,
                districtBelong: v.id,
                districtBelongName: v.name,
                enterpriseNumber: v.enterpriseNumber,
                water: v.water,
                electric: v.electricity,
                coal: v.coal,
                gas: v.gas,
                consumption: v.comprehensiveEnergyConsumption,
                maxConsumptionEnterprise: v.bigEnergyConsumer
              }))
            );
          }
        });
      }
    },
    GET_INDUSTRY_MAP_LIST({ commit, state }) {
      const lists = state.industryMapList;
      if (!lists.length) {
        loadIndustryMapData().then(res => {
          if (res.code === 200) {
            res.data = res.data || [];
            commit(
              'SET_INDUSTRY_MAP_LIST',
              res.data.map(v => ({
                _name: v.name,
                belongIndustry: v.id,
                belongIndustryName: v.name,
                enterpriseNumber: v.enterpriseNumber,
                water: v.water,
                electric: v.electricity,
                gas: v.gas,
                consumption: v.comprehensiveEnergyConsumption,
                maxConsumptionEnterprise: v.bigEnergyConsumer
              }))
            );
          }
        });
      }
    }
  },
  modules: {
    user,
    tagsView
  }
});
